package smack.client.gui;

import com.senyint.imsdk.listener.SdkHandlerAdapter;
import com.senyint.imsdk.packet.AttachmentPacketExtension;
import com.senyint.imsdk.packet.SimpleEventIQ;

import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;

public class MySdkHandlerListener extends SdkHandlerAdapter {
    private static final String TAG = "MySdkHandlerListener";

    public MySdkHandlerListener(){
        super();
    }

    @Override
    public void handleLoginSuccess(String uid, String token) {
        super.handleLoginSuccess(uid, token);
        log("handleLoginSuccess:"+uid);
    }

    @Override
    public void handleLoginFailed(Exception e) {
        super.handleLoginFailed(e);
        log("handleLoginFailed:"+e.getMessage());
    }

    @Override
    public void handleConnectionClosedOnError(Exception ex) {
        super.handleConnectionClosedOnError(ex);
        log("handleLoginFailed:"+ex.getMessage());
    }

    @Override
    public void handleConnectionClosed() {
        super.handleConnectionClosed();
        log("handleConnectionClosed:" );
    }

    @Override
    public void handleReconnectionFailed(Exception e) {
        super.handleReconnectionFailed(e);
        log("handleReconnectionFailed:"+e.getMessage() );
    }

    @Override
    public void handleReconnectionSuccessful() {
        super.handleReconnectionSuccessful();
        log("handleReconnectionSuccessful:" );
    }

    @Override
    public void handlePacketBeforeSent(Packet packet) {
        super.handlePacketBeforeSent(packet);
        log("handlePacketBeforeSent:" + packet.toXML() );
        _handleBeforeSent(packet);
    }

    @Override
    public void handleReceivedEvent(SimpleEventIQ packet) {
        super.handleReceivedEvent(packet);
        log("handleReceivedEvent:" + packet.toXML());
    }

    @Override
    public void handleReceivedPacket(Packet packet) {
        super.handleReceivedPacket(packet);
        log("handleReceivedPacket:" + packet.toXML());
        _handleRecv(packet);
    }

    @Override
    public void handleUserConflict() {
        super.handleUserConflict();
        log("handleUserConflict:" );

    }

    private void log(String s) {
        log(TAG,"-->"+s);
    }




    private void _handleBeforeSent(Packet packet) {
        log(TAG,"intercept before send ----->"+ packet.toXML());
    }


    private void _handleRecv(Packet packet) {
        if(packet instanceof Message) {
            Message message = (Message) packet;
            System.out.println("aaaaaaaaaaaaaaaaaaaaaaa::" + message.toXML());
            log(TAG, "-####### check user:" + message.toXML());
        }
    }


    private void log(String tag2, String s) {
    	 System.out.println(tag2 + "---->>>>"+s);		
	}

	public void onUpdateReceivedMessageState(String packetID) {
    }

    public void onUpdateSentMessage(String packetID) {
    }


}
